home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / src / gen / Vec.hP < prev    next >
Text File  |  1994-06-28  |  3KB  |  136 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19.  
  20. #ifndef _<T>Vec_h
  21. #ifdef __GNUG__
  22. #pragma interface
  23. #endif
  24. #define _<T>Vec_h 1
  25.  
  26. #include <builtin.h>
  27. #include "<T>.defs.h"
  28.  
  29. #ifndef _<T>_typedefs
  30. #define _<T>_typedefs 1
  31. typedef void (*<T>Procedure)(<T&>);
  32. typedef <T>  (*<T>Mapper)(<T&>);
  33. typedef <T>  (*<T>Combiner)(<T&>, <T&>);
  34. typedef int  (*<T>Predicate)(<T&>);
  35. typedef int  (*<T>Comparator)(<T&>, <T&>);
  36. #endif
  37.  
  38.  
  39. class <T>Vec 
  40. {
  41. protected:      
  42.   int                   len;
  43.   <T>                   *s;                  
  44.  
  45.                         <T>Vec(int l, <T>* d);
  46. public:
  47.                         <T>Vec ();
  48.                         <T>Vec (int l);
  49.                         <T>Vec (int l, <T&> fill_value);
  50.                         <T>Vec (const <T>Vec&);
  51.                         ~<T>Vec ();
  52.  
  53.   <T>Vec &              operator = (const <T>Vec & a);
  54.   <T>Vec                at(int from = 0, int n = -1);
  55.  
  56.   int                   capacity() const;
  57.   void                  resize(int newlen);                        
  58.  
  59.   <T>&                  operator [] (int n);
  60.   <T>&                  elem(int n);
  61.  
  62.   friend <T>Vec         concat(<T>Vec & a, <T>Vec & b);
  63.   friend <T>Vec         map(<T>Mapper f, <T>Vec & a);
  64.   friend <T>Vec         merge(<T>Vec & a, <T>Vec & b, <T>Comparator f);
  65.   friend <T>Vec         combine(<T>Combiner f, <T>Vec & a, <T>Vec & b);
  66.   friend <T>Vec         reverse(<T>Vec & a);
  67.  
  68.   void                  reverse();
  69.   void                  sort(<T>Comparator f);
  70.   void                  fill(<T&> val, int from = 0, int n = -1);
  71.  
  72.   void                  apply(<T>Procedure f);
  73.   <T>                   reduce(<T>Combiner f, <T&> base);
  74.   int                   index(<T&> targ);
  75.  
  76.   friend int            operator == (<T>Vec& a, <T>Vec& b);
  77.   friend int            operator != (<T>Vec& a, <T>Vec& b);
  78.  
  79.   void                  error(const char* msg);
  80.   void                  range_error();
  81. };
  82.  
  83. extern void default_<T>Vec_error_handler(const char*);
  84. extern one_arg_error_handler_t <T>Vec_error_handler;
  85.  
  86. extern one_arg_error_handler_t 
  87.         set_<T>Vec_error_handler(one_arg_error_handler_t f);
  88.  
  89.  
  90. inline <T>Vec::<T>Vec()
  91. {
  92.   len = 0; s = 0;
  93. }
  94.  
  95. inline <T>Vec::<T>Vec(int l)
  96. {
  97.   s = new <T> [len = l];
  98. }
  99.  
  100.  
  101. inline <T>Vec::<T>Vec(int l, <T>* d) :len(l), s(d) {}
  102.  
  103.  
  104. inline <T>Vec::~<T>Vec()
  105. {
  106.   delete [] s;
  107. }
  108.  
  109.  
  110. inline <T>& <T>Vec::operator [] (int n)
  111. {
  112.   if ((unsigned)n >= (unsigned)len)
  113.     range_error();
  114.   return s[n];
  115. }
  116.  
  117. inline <T>& <T>Vec::elem(int n)
  118. {
  119.   return s[n];
  120. }
  121.  
  122.  
  123. inline int <T>Vec::capacity() const
  124. {
  125.   return len;
  126. }
  127.  
  128.  
  129.  
  130. inline int operator != (<T>Vec& a, <T>Vec& b)
  131. {
  132.   return !(a == b);
  133. }
  134.  
  135. #endif
  136.